<template>
  <div class="feed-tab">
    <el-card class="feed-header">
      <div class="feed-info">
        <h3>关注动态</h3>
        <p>查看您关注的用户的最新分享</p>
      </div>
      <el-button @click="refreshFeed">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </el-card>

    <div class="feed-list" v-loading="loading">
      <div v-if="feedItems.length === 0" class="empty-state">
        <el-empty description="暂无动态">
          <el-button type="primary" @click="goToDiscover">去发现更多用户</el-button>
        </el-empty>
      </div>

      <div v-else>
        <div 
          v-for="item in feedItems" 
          :key="item.id"
          class="feed-item"
        >
          <el-card shadow="hover">
            <div class="feed-content">
              <div class="user-info">
                <img 
                  :src="item.user?.avatar || '/placeholder-avatar.png'" 
                  :alt="item.user?.username"
                  class="user-avatar"
                />
                <div class="user-details">
                  <span class="username">{{ item.user?.username }}</span>
                  <span class="action-text">分享了新搭配</span>
                  <span class="publish-time">{{ formatDate(item.created_at) }}</span>
                </div>
              </div>

              <div class="outfit-preview" v-if="item.outfit">
                <img 
                  :src="item.outfit.main_image || '/placeholder-outfit.png'" 
                  :alt="item.outfit.name"
                  class="outfit-image"
                  @click="viewOutfit(item.outfit)"
                />
                <div class="outfit-info">
                  <h4>{{ item.outfit.name }}</h4>
                  <p v-if="item.caption">{{ item.caption }}</p>
                  <div class="outfit-stats">
                    <span><el-icon><Star /></el-icon> {{ item.like_count || 0 }}</span>
                    <span><el-icon><ChatDotRound /></el-icon> {{ item.comment_count || 0 }}</span>
                    <span><el-icon><View /></el-icon> {{ item.view_count || 0 }}</span>
                  </div>
                </div>
              </div>

              <div class="feed-actions">
                <el-button size="small" @click="likeItem(item)">
                  <el-icon><Star /></el-icon>
                  点赞
                </el-button>
                <el-button size="small" @click="commentItem(item)">
                  <el-icon><ChatDotRound /></el-icon>
                  评论
                </el-button>
                <el-button size="small" @click="shareItem(item)">
                  <el-icon><Share /></el-icon>
                  分享
                </el-button>
              </div>
            </div>
          </el-card>
        </div>
      </div>
    </div>

    <div class="load-more" v-if="hasMore">
      <el-button @click="loadMore" :loading="loadingMore">
        加载更多
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { socialAPI } from '@/api'

const loading = ref(false)
const loadingMore = ref(false)
const feedItems = ref([])
const currentPage = ref(1)
const hasMore = ref(true)

const loadFeed = async (reset = true) => {
  try {
    if (reset) {
      loading.value = true
      currentPage.value = 1
    } else {
      loadingMore.value = true
    }

    const response = await socialAPI.getFeed({
      page: currentPage.value,
      pageSize: 20
    })

    if (response.code === 200) {
      const newItems = response.data.list || []
      
      if (reset) {
        feedItems.value = newItems
      } else {
        feedItems.value.push(...newItems)
      }

      hasMore.value = newItems.length === 20
    }
  } catch (error) {
    console.error('获取动态失败:', error)
    ElMessage.error('获取动态失败')
  } finally {
    loading.value = false
    loadingMore.value = false
  }
}

const refreshFeed = () => {
  loadFeed(true)
}

const loadMore = () => {
  currentPage.value++
  loadFeed(false)
}

const viewOutfit = (outfit) => {
  console.log('查看搭配:', outfit)
}

const likeItem = (item) => {
  console.log('点赞:', item)
}

const commentItem = (item) => {
  console.log('评论:', item)
}

const shareItem = (item) => {
  console.log('分享:', item)
}

const goToDiscover = () => {
  // 切换到发现页面
  console.log('跳转到发现页面')
}

const formatDate = (dateString) => {
  const date = new Date(dateString)
  const now = new Date()
  const diffTime = Math.abs(now - date)
  const diffHours = Math.ceil(diffTime / (1000 * 60 * 60))
  
  if (diffHours < 1) return '刚刚'
  if (diffHours < 24) return `${diffHours}小时前`
  
  const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24))
  if (diffDays < 7) return `${diffDays}天前`
  
  return date.toLocaleDateString('zh-CN')
}

onMounted(() => {
  loadFeed()
})
</script>

<style scoped>
.feed-tab {
  space-y: 20px;
}

.feed-header {
  margin-bottom: 20px;
}

.feed-header .el-card__body {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.feed-info h3 {
  margin: 0 0 4px 0;
  color: #333;
}

.feed-info p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.feed-list {
  space-y: 16px;
}

.feed-item {
  margin-bottom: 16px;
}

.feed-content {
  padding: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.username {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.action-text {
  font-size: 14px;
  color: #666;
  margin: 2px 0;
}

.publish-time {
  font-size: 12px;
  color: #999;
}

.outfit-preview {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.outfit-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  cursor: pointer;
}

.outfit-info {
  flex: 1;
}

.outfit-info h4 {
  margin: 0 0 8px 0;
  color: #333;
}

.outfit-info p {
  margin: 0 0 12px 0;
  color: #666;
  line-height: 1.5;
}

.outfit-stats {
  display: flex;
  gap: 16px;
}

.outfit-stats span {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #666;
}

.feed-actions {
  display: flex;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid #eee;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.load-more {
  text-align: center;
  margin: 40px 0;
}

@media (max-width: 768px) {
  .outfit-preview {
    flex-direction: column;
  }
  
  .outfit-image {
    width: 100%;
    height: 200px;
  }
  
  .feed-actions {
    flex-wrap: wrap;
  }
}
</style>
